import { v4 as uuidv4 } from 'uuid';

/**
 * 生成设备ID
 * @returns {string} 设备ID
 */
export function generateDeviceId() {
  // 使用UUID生成唯一标识
  return uuidv4();
}

/**
 * 生成设备名称
 * @returns {string} 设备名称
 */
export function generateDeviceName() {
  // 获取操作系统信息
  const userAgent = navigator.userAgent;
  let osName = 'Unknown';
  
  if (userAgent.indexOf('Win') !== -1) osName = 'Windows';
  else if (userAgent.indexOf('Mac') !== -1) osName = 'MacOS';
  else if (userAgent.indexOf('Linux') !== -1) osName = 'Linux';
  else if (userAgent.indexOf('Android') !== -1) osName = 'Android';
  else if (userAgent.indexOf('iPhone') !== -1 || userAgent.indexOf('iPad') !== -1) osName = 'iOS';
  
  // 获取浏览器信息
  let browserName = 'Unknown';
  
  if (userAgent.indexOf('Chrome') !== -1) browserName = 'Chrome';
  else if (userAgent.indexOf('Firefox') !== -1) browserName = 'Firefox';
  else if (userAgent.indexOf('Safari') !== -1) browserName = 'Safari';
  else if (userAgent.indexOf('Edge') !== -1) browserName = 'Edge';
  else if (userAgent.indexOf('MSIE') !== -1 || userAgent.indexOf('Trident/') !== -1) browserName = 'IE';
  
  // 创建随机数
  const randomNum = Math.floor(Math.random() * 10000);
  
  // 组合设备名称
  return `${osName}-${browserName}-${randomNum}`;
}

/**
 * 获取设备名称
 * 根据系统和浏览器信息生成默认设备名称
 */
export function getDeviceName() {
  const platform = getPlatformInfo()
  const browser = getBrowserInfo()
  return `${platform}上的${browser}`
}

/**
 * 获取平台信息
 */
function getPlatformInfo() {
  const userAgent = navigator.userAgent
  let platform = 'Unknown'
  
  if (/Windows/.test(userAgent)) {
    platform = 'Windows'
  } else if (/Macintosh|MacIntel|MacPPC|Mac68K/.test(userAgent)) {
    platform = 'Mac'
  } else if (/iPhone|iPad|iPod/.test(userAgent)) {
    platform = 'iOS'
  } else if (/Android/.test(userAgent)) {
    platform = 'Android'
  } else if (/Linux/.test(userAgent)) {
    platform = 'Linux'
  }
  
  return platform
}

/**
 * 获取浏览器信息
 */
function getBrowserInfo() {
  const userAgent = navigator.userAgent
  let browser = 'Unknown Browser'
  
  if (/Edge/.test(userAgent)) {
    browser = 'Edge'
  } else if (/Chrome/.test(userAgent)) {
    browser = 'Chrome'
  } else if (/Firefox/.test(userAgent)) {
    browser = 'Firefox'
  } else if (/Safari/.test(userAgent)) {
    browser = 'Safari'
  } else if (/MSIE|Trident/.test(userAgent)) {
    browser = 'IE'
  }
  
  return browser
}

/**
 * 格式化IP地址
 */
export function formatIpAddress(ip) {
  if (!ip) return '未知'
  return ip
}

/**
 * 格式化设备状态
 */
export function formatDeviceStatus(status) {
  return status === 1 ? '在线' : '离线'
} 